/*
 * Copyright (C) 2008  YATR.Main@gmail.com
 *
 * $Id$
 * You may distribute under the GPL-2 License , as specified in 
 * the README.txt file.
 *
 * For more information about YATR, or for information on how to 
 * contact the author, see the README.txt file.
 */

package org.yatr.tci;

import org.etsi.ttcn.tci.TciTypeClass;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;
import org.etsi.ttcn.tci.VerdictValue;
//import static org.etsi.ttcn.tci.VerdictValue.*;
import org.yatr.exception.TtcnEncodedFailureException;
import org.yatr.exception.TtcnUnexceptedTypeException;

/**
 * @author YATR.Main@gmail.com
 *
 */
public class VerdictValueYatr extends Variable implements VerdictValue {

	public VerdictValueYatr() {
		mValue = NONE;
		setIsOmit(false);
	}
	
	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.VerdictValue#getVerdict()
	 */
	public int getVerdict() {
		checkOmit(this);
		return mValue;
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.VerdictValue#setVerdict(int)
	 */
	public void setVerdict(int value) {
		if(value < NONE || value > ERROR)
			throw new IllegalArgumentException("unknown verdict="+mValue);
		mValue = value;
		setIsOmit(false);
	}

	@Override
	public void assign(Object... objs) {
		checkObjectArray(objs);
		Object o = objs[0];
		
		if(o instanceof VerdictValueYatr) {
			VerdictValueYatr o2 = (VerdictValueYatr)o;
			checkOmit(o2);
			mValue = o2.mValue;
		} else if(o instanceof Integer) {
			mValue = (Integer)o;
		} else {
			checkTypeException(o);
		}
		setIsOmit(false);
	}

	@Override
	public void encode(BitBuffer buf) {
		throw new TtcnUnexceptedTypeException("verdicttype cannot encode/decode");
	}

	@Override
	public boolean equals(Object o) {
		checkNull(o);
		checkOmit(this);
		
		if(o instanceof VerdictValueYatr) {
			VerdictValueYatr o2 = (VerdictValueYatr)o;
			checkOmit(o2);
			return mValue == o2.mValue;
		} else if(o instanceof Integer) {
			return mValue == (Integer)o;
		} else {
			checkTypeException(o);
		}
		return false;

	}

	@Override
	public String toString() {
		if(getIsOmit())
			return UNDEFINED;
		else {
			if(mValue == NONE) 
				return "none";
			else if(mValue == PASS) 
				return "pass";
			else if(mValue == INCONC)
				return "inconc";
			else if(mValue == FAIL)
				return "fail";
			else if(mValue == ERROR)
				return "error";
			else
				throw new IllegalArgumentException("unknown verdict="+mValue);
		}
	}

	public String getName() {
		return "verdict";
	}

	public int getTypeClass() {
		return TciTypeClass.VERDICT;
	}

	public Value newInstance() {
		return new VerdictValueYatr();
	}
	
	private int mValue;

}
